package com.zhuimeng.redis;

import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.LinkedHashSet;
import java.util.Set;

public class RedisTest {

    JedisCluster masterCluster = null;
    JedisCluster slaveCluster = null;

    @Before
    public void init(){
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        // 最大连接数
        poolConfig.setMaxTotal(1);
        // 最大空闲数
        poolConfig.setMaxIdle(1);
        // 最大允许等待时间，如果超过这个时间还未获取到连接，则会报JedisException异常：
        // Could not get a resource from the pool
        poolConfig.setMaxWaitMillis(1000);

        Set<HostAndPort> masterNodes = new LinkedHashSet<HostAndPort>();
        masterNodes.add(new HostAndPort("192.168.56.103", 6381));
        masterNodes.add(new HostAndPort("192.168.56.103", 6382));
        masterNodes.add(new HostAndPort("192.168.56.103", 6383));
        masterNodes.add(new HostAndPort("192.168.56.103", 6387));

        Set<HostAndPort> slaveNodes = new LinkedHashSet<HostAndPort>();
        slaveNodes.add(new HostAndPort("192.168.56.103", 6384));
        slaveNodes.add(new HostAndPort("192.168.56.103", 6385));
        slaveNodes.add(new HostAndPort("192.168.56.103", 6386));
        slaveNodes.add(new HostAndPort("192.168.56.103", 6387));
        masterCluster = new JedisCluster(masterNodes,poolConfig);
        slaveCluster = new JedisCluster(slaveNodes,poolConfig);
    }

    @Test
    public void put(){
        masterCluster.set("a","ccccccc");
        System.out.println("数据添加成功");
    }
    @Test
    public void get(){
        System.out.println(slaveCluster.get("a"));;
    }
}
